package se.slackers.sounder.sound;

public class Signal {
	private final double frequency;
	private final double volume;
	private final double dt;

	private double time = 0;

	public static Signal create(final int note, final double volume, final double samplerate) {
		final double frequency = 65.41 * Math.pow(2, note / 12.0);
		return new Signal(frequency, volume, samplerate);
	}

	private Signal(final double frequency, final double volume, final double samplerate) {
		this.frequency = frequency;
		this.volume = volume;
		this.dt = 1.0 / samplerate;
	}

	public double generate() {
		time += dt;
		return Math.sin(frequency * 2 * Math.PI * time) * volume;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(frequency);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Signal other = (Signal) obj;
		if (Double.doubleToLongBits(frequency) != Double.doubleToLongBits(other.frequency)) {
			return false;
		}
		return true;
	}

}
